<?php
/**
 * @author krku
 */

function smarty_block_insertPhotoCropper($params, $content, &$smarty, &$repeat) {

	if (!$repeat) {

		if (isset($params['preview'])) {
			if (!is_string($params['preview'])) {
				$preview = $params['prefix'] . "CropPreview";
			} else {
				$preview = $params['preview'];
			}
		}

		$width = $smarty->get_template_vars('photoWidth');
		$height = $smarty->get_template_vars('photoHeight');

		$minWidth = $smarty->get_template_vars('minPhotoWidth');
		$minHeight = $smarty->get_template_vars('minPhotoHeight');

		$uploadUrl = Router::GenerateUrl('uploadUrl', $smarty->get_template_vars('uploadUrl'));
		$cutUrl = Router::GenerateUrl('cutUrl', $smarty->get_template_vars('cutUrl'));

		if (isset($params['loadOnFly']) && $params['loadOnFly']) {
			$onFly = true;
			$uploadForm = '
			<div class="photoUploadForm" id="' . $params['prefix'] . 'PhotoUploadForm">
				<form onsubmit="return false;" action="' . $uploadUrl . '" method="post" name="' . $params['prefix'] . 'PhotoUpload" target="' . $params['prefix'] . 'PhotoUploadIFrame" enctype="multipart/form-data">
					<input type="file" name="photo" onchange="startUpload(\'' . $params['prefix'] . 'PhotoUploadIFrame\', this.form);" title="" />
					<input name="onFly" value="1" type="hidden" />
					<input name="cropPrefix" value="' . $params['prefix'] . '" type="hidden" />
				</form>
				<iframe src="puste" id="' . $params['prefix'] . 'PhotoUploadIFrame" name="' . $params['prefix'] . 'PhotoUploadIFrame" class="photoUpload" frameborder="0" width="1" height="1"></iframe>
			</div>';
		} else {
			$obFly = false;
		}

		$photoCropper = '
			
				<div class="cropperMainPhotoFrame">
					<img src="' . ($onFly ? $smarty->get_template_vars('noPhotoImgBig') : '') . '" id="' . $params['prefix'] . 'CropArea" class="PhotoBig" ';

		if ($height != false && $width != false) {
			$photoCropper .= 'style="height: ' . $height . 'px; width: ' . $width . 'px;"';
		}

		$photoCropper .= ' />
				</div>
				<div class="cropperInfoFrame">';

		if (isset($preview)) {
			$photoCropper .= '
					<h4>Podgląd</h4>
					<div id="' . $preview . '" class="' . $params['prefix'] . 'PhotoSmall">
						<img id="' . $params['prefix'] . 'PreviewArea" src="' . ($onFly ? $smarty->get_template_vars('noPhotoImgSmall') : '') . '" />
					</div>
					Wybierz odpowiedni kadr przesuwając i regulując krawędzie kwadratowego obszaru.';
		}

		if (isset($params['viewForm']) && $params['viewForm']) {
			$type = "text";
		} else {
			$type = "hidden";
		}

		$desc = '';
		if(isset($params['description']) && $params['description'] == true) {
			$desc = '<textarea name="' . $params['prefix'] . 'Description" id="' . $params['prefix'] . 'Description" class="cropper"></textarea>';
		} else {
			$desc = '<input type="hidden" name="' . $params['prefix'] . 'Description" id="' . $params['prefix'] . 'Description" value="0" />';
		}

		$photoCropper .= '
					<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropX1" id="' . $params['prefix'] . 'ImageCropX1" />
					<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropX2" id="' . $params['prefix'] . 'ImageCropX2" />
					<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropY1" id="' . $params['prefix'] . 'ImageCropY1" />
					<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropY2" id="' . $params['prefix'] . 'ImageCropY2" />
					<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropWidth" id="' . $params['prefix'] . 'ImageCropWidth" />
					<input type="' . $type . '" name="' . $params['prefix'] . 'ImageCropHeight" id="' . $params['prefix'] . 'ImageCropHeight" />
					<input type="hidden" name="' . $params['prefix'] . 'ImageFileName" id="' . $params['prefix'] . 'ImageFileName" value="' . $params['photoSrc'] . '"/>
				</div>
				' . $desc . '
				<div class="clearBoth"></div>
				';

		$formBegin = '<form name="photoCropper" action="' . $cutUrl . '" method="post" onsubmit="doCropPhoto(this, \'photoCropper\'); return false;">';

		$content = str_replace('%formBegin%', $formBegin, $content);
		$content = str_replace('%formEnd%', '</form>', $content);
		$content = str_replace('%photoCropper%', $photoCropper, $content);
		$content = str_replace('%uploadForm%', $uploadForm, $content);
		$content = str_replace('%errorAreaId%', $params['prefix'] . 'ErrorArea', $content);

		$content .= '<script type="text/javascript" language="javascript">'."
				initializeCropper('$preview', '$params[prefix]', $minWidth, $minHeight, $params[onInit], " . ($params['loadOnFly'] ? 'true' : 'false') . ");
			</script>";

		return $content;
	}
}
?>